package com.example.program.demos.controller;

import com.example.program.demos.request.RequestParams;
import com.example.program.demos.request.UserParams;
import com.example.program.demos.service.TianXingService;
import com.example.program.demos.utils.JsonUtil;
import com.example.program.exception.ErrorRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tx")
public class TianXingController {

    @Autowired
    TianXingService tianXingService;

    @RequestMapping(value = "/select", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String selectAll(HttpServletRequest request) throws ParseException {
        int pageStart = Integer.parseInt(request.getParameter("pageStart"));
        int pageLength = Integer.parseInt(request.getParameter("pageLength"));
        String company = request.getParameter("companyId");
        Integer companyId = StringUtils.isEmpty(company) ? null : Integer.parseInt(company);
        String program = request.getParameter("programId");
        Integer programId = StringUtils.isEmpty(program)? null : Integer.parseInt(program);
        String group = StringUtils.isEmpty(request.getParameter("group")) ? null : request.getParameter("group");
//        group = StringUtils.isEmpty(group) ? null : group;
        String months = request.getParameter("month");
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
        map.put("programId", programId);
        map.put("group", group);
        map.put("pageStart", pageStart);
        map.put("pageLength", pageLength);
        map.put("month", new SimpleDateFormat("yyyy-MM").parse(months));
        List<Map<String, Object>> list = tianXingService.selectAll(map);
        int totalSize = tianXingService.selectAllCounts(map);
        Map<String, Object> result = new HashMap<>();
        result.put("totalSize", totalSize);
        result.put("aData", list);

        return JsonUtil.writeObjectAsString(result);
    }

    @RequestMapping(value = "/insertAttendance", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public ErrorRes insertAttendance(@RequestBody RequestParams requestParams) {
        try {
            List<Map<String, Object>> maps = requestParams.getTotalAttendances();
//            List<TotalAttendance> totalAttendances = requestParams.getTotalAttendances();
            Map<String, Object> tableInf = requestParams.getTableInf();
            Date month = new SimpleDateFormat("yyyy-MM").parse(tableInf.get("month").toString());
            for (Map<String, Object> map: maps) {
//               TotalAttendance mmm = totalAttendance;
//                totalAttendance.setMonth(month);
                map.put("month", month);
                map.put("companyId", tableInf.get("company"));
                map.put("programId", tableInf.get("programm"));
                tianXingService.insertAttendance(map);
            }
            return new ErrorRes( "添加成功",0);
        } catch (Exception e) {
            return new ErrorRes(e.getCause().getMessage(), -1);
        }
    }

    @RequestMapping(value = "/insertUser", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public ErrorRes insertUser(@RequestBody UserParams userParams) {
        try {
            List<Map<String, Object>> totalUser = userParams.getTotalUser();
            Map<String, Object> userInf = userParams.getUserInf();

            for (Map<String, Object> map: totalUser) {
//               TotalAttendance mmm = totalAttendance;
//                totalAttendance.setMonth(month);

                tianXingService.insertUser(map, userInf);
            }
            return new ErrorRes( "添加成功",0);
        } catch (Exception e) {
            return new ErrorRes(e.getCause().getMessage(), -1);
        }
    }



    @RequestMapping(value = "/updateUser", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public ErrorRes updateUser(@RequestBody UserParams userParams) {
        try {
            Map<String, Object> userInf = userParams.getUserInf();
            tianXingService.updateUser(userInf);
            tianXingService.updateUserAttendance(userInf);
            return new ErrorRes( "添加成功",0);
        } catch (Exception e) {
            return new ErrorRes(e.getCause().getMessage(), -1);
        }
    }



}
